#ifndef SCStatusTcp_H
#define SCStatusTcp_H

#include <QTcpSocket>
#include <QObject>
#include <QElapsedTimer>

class SCStatusTcp : public QObject
{
    Q_OBJECT
public:
    explicit SCStatusTcp(QObject *parent = 0);

    ~SCStatusTcp();
    void asyncWriteTcpData(uint16_t cmd, const QByteArray &data, uint16_t reqId);  //异步发送接口
    void releaseTcpSocket();
    QString lastError() const;

    void setLastError(const QString &lastError);

    bool writeTcpData(uint16_t sendCommand,
                      const QByteArray &jsonData,
                      const QByteArray &sendData,
                      uint16_t &number, uint8_t byte15);

    QTcpSocket *tcpSocket() const;

    QElapsedTimer time() const;

    int connectHost(const QString &ip, quint16 port);
    QString getCurrentDateTime() const;
    QString hexToQString(const QByteArray &b);
    int getTimeOut() const;
    void setTimeOut(int timeOut);

public slots:
    void receiveTcpReadyRead();
private slots:
    void stateChanged(QAbstractSocket::SocketState state);
    void receiveTcpError(QAbstractSocket::SocketError error);
    
signals:
    void sigPrintInfo(QString);
    void sigChangedText(bool ,int ,QByteArray ,QByteArray ,int ,int );
    void responseReceived(uint16_t cmd, uint16_t reqId, const QByteArray &data); //响应信号

    private:
    QTcpSocket *_tcpSocket;
    QString _lastError;
    QString _ip; //ip
    quint16 _port; //端口号
    uint16_t _number;//序号

    QByteArray _lastMessage;//存放所有读取的数据
    QElapsedTimer _time;//用来监测时间

    int _oldSendCommand;
    int _oldNumber;
    int _timeOut;
    QMap<uint16_t, uint16_t> m_reqIdCmdMap; // reqId到cmd的映射
    QString formatHex(const QByteArray &data) const;

};

#endif // SCStatusTcp_H
